package gui2.g3;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public class ChatGUI extends JFrame {

	private JLabel lblCaption;

	private JLabel lblUsername;
	private JTextField txtUsername;
	private JButton btnStartChat;

	private JList list;
	private JScrollPane listScroll;

	private JTable table;

	private JTextField txtMessage;
	private JButton btnSendMessage;

	private ChatModel chatModel = new ChatModel();

	public ChatGUI() {
		setTitle("Chat");
		setDefaultCloseOperation(EXIT_ON_CLOSE);

		createWidgets();
		layoutWidgets();
		setupInteraction();

		pack();
		setLocationRelativeTo(null);
	}

	private void setupInteraction() {
		btnSendMessage.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String text = txtMessage.getText();
				chatModel.addMessage(new ChatMessage(text, txtUsername
						.getText()));
			}
		});

		// txtMessage.addCaretListener(new CaretListener(){
		// {
		// caretUpdate(null);
		// }
		//
		// @Override
		// public void caretUpdate(CaretEvent e) {
		// String text = txtMessage.getText();
		// boolean enableButton = text != null && !text.trim().isEmpty();
		// btnSendMessage.setEnabled(enableButton);
		// }
		// });

		txtMessage.getDocument().addDocumentListener(new DocumentListener() {

			{
				updateButton();
			}

			private void updateButton() {
				String text = txtMessage.getText();
				boolean enableButton = text != null && !text.trim().isEmpty();
				btnSendMessage.setEnabled(enableButton);
			}

			@Override
			public void removeUpdate(DocumentEvent e) {
				updateButton();
			}

			@Override
			public void insertUpdate(DocumentEvent e) {
				updateButton();
			}

			@Override
			public void changedUpdate(DocumentEvent e) {
				updateButton();
			}
		});

		getContentPane().addMouseMotionListener(new MouseAdapter() {

			@Override
			public void mouseMoved(MouseEvent e) {
				Point mousePos = e.getPoint();
				if (mousePos.x <= 10
						&& mousePos.y >= getContentPane().getSize().height - 10) {

					chatModel.addMessage(new ChatMessage("AFK", txtUsername
							.getText()));
				}
			}
		});
	}

	private void createWidgets() {
		lblCaption = new JLabel("CHAT");
		lblCaption.setHorizontalAlignment(SwingConstants.CENTER);

		lblUsername = new JLabel("Username:");
		txtUsername = new JTextField();
		btnStartChat = new JButton("Start");

		ChatListModel listModel = new ChatListModel();
		chatModel.addListener(listModel);

		list = new JList(listModel);
		list.setBorder(BorderFactory.createLineBorder(Color.RED));
		listScroll = new JScrollPane(list);
		listScroll.setPreferredSize(new Dimension(500, 500));
		list.setCellRenderer(new ChatListRenderer());

		ChatTableModel tableModel = new ChatTableModel();
		chatModel.addListener(tableModel);

		table = new JTable(tableModel);

		txtMessage = new JTextField();
		btnSendMessage = new JButton("Send");
	}

	private void layoutWidgets() {
		getContentPane().setLayout(new GridBagLayout());

		GridBagConstraints c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 0;
		c.gridwidth = 3;
		c.gridheight = 1;
		c.fill = GridBagConstraints.HORIZONTAL;
		getContentPane().add(lblCaption, c);

		c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 1;
		c.gridwidth = 1;
		c.gridheight = 1;
		getContentPane().add(lblUsername, c);

		c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 2;
		c.gridwidth = 1;
		c.gridheight = 1;
		c.fill = GridBagConstraints.HORIZONTAL;
		getContentPane().add(txtUsername, c);

		c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 3;
		c.gridwidth = 1;
		c.gridheight = 1;
		c.fill = GridBagConstraints.HORIZONTAL;
		c.anchor = GridBagConstraints.BASELINE;
		getContentPane().add(btnStartChat, c);

		c = new GridBagConstraints();
		c.gridx = 1;
		c.gridy = 1;
		c.gridwidth = 1;
		c.gridheight = 3;
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 1;
		c.weighty = 1;
		c.insets = new Insets(5, 5, 5, 5);
		getContentPane().add(listScroll, c);

		c = new GridBagConstraints();
		c.gridx = 2;
		c.gridy = 1;
		c.gridwidth = 1;
		c.gridheight = 3;
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 1;
		c.weighty = 1;
		c.insets = new Insets(5, 5, 5, 5);
		getContentPane().add(table, c);

		c = new GridBagConstraints();
		c.gridx = 1;
		c.gridy = 4;
		c.gridwidth = 1;
		c.gridheight = 1;
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 1;
		getContentPane().add(txtMessage, c);

		c = new GridBagConstraints();
		c.gridx = 2;
		c.gridy = 4;
		c.gridwidth = 1;
		c.gridheight = 1;
		getContentPane().add(btnSendMessage, c);
	}

	public static void main(String[] args) {

		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				
				try {
					UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				} catch (InstantiationException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (UnsupportedLookAndFeelException e) {
					e.printStackTrace();
				}

				int answer = JOptionPane.showConfirmDialog(null,
						"Wollen Sie den Chat betreten?");
				if (answer == JOptionPane.YES_OPTION) {
					JFrame f = new ChatGUI();
					f.setVisible(true);
				}
			}
		});

	}
}
